/**
 * CaddieTransferableList.java 21 juil. 07
 *
 * Copyright (c) 2007 windu.2b <jcaddie@windu2b.org>. All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.windu2b.jcaddie.swing;

import java.awt.datatransfer.*;
import java.io.*;
import java.util.*;

import org.windu2b.jcaddie.model.*;

/**
 * @author windu.2b
 * 
 */
public class CaddieTransferableList implements Transferable
{
	public final static DataFlavor	CADDIE_FLAVOR;

	static
	{
		try
		{
			String caddieFlavorMimeType = DataFlavor.javaJVMLocalObjectMimeType
			        + ";class=" + CaddieTransferableList.class.getName();

			CADDIE_FLAVOR = new DataFlavor( caddieFlavorMimeType );
		}
		catch ( ClassNotFoundException ex )
		{
			throw new RuntimeException( ex );
		}
	}


	private List<Object>	       transferedItems;




	public CaddieTransferableList( List<? extends Object> items )
	{
		transferedItems = deepCopy( items );
	}




	private List<Object> deepCopy( List<? extends Object> items )
	{
		List<Object> list = new ArrayList<Object>();
		for ( Object item : items )
		{
			if ( item instanceof PieceOfProduct )
			{
				list.add( new CaddiePieceOfProduct( ( PieceOfProduct ) item ) );
			}
			else
			{
				throw new RuntimeException(
				        "HomeTransferableList can't contain "
				                + item.getClass().getName() );
			}
		}

		return list;
	}




	/**
	 * @see java.awt.datatransfer.Transferable#getTransferData(java.awt.datatransfer.DataFlavor)
	 */
	public Object getTransferData( DataFlavor flavor )
	        throws UnsupportedFlavorException, IOException
	{
		if ( flavor.equals( CADDIE_FLAVOR ) )
			return deepCopy( transferedItems );
		else throw new UnsupportedFlavorException( flavor );
	}




	/**
	 * @see java.awt.datatransfer.Transferable#getTransferDataFlavors()
	 */
	public DataFlavor[] getTransferDataFlavors()
	{
		return new DataFlavor[] { CADDIE_FLAVOR };
	}




	/**
	 * @see java.awt.datatransfer.Transferable#isDataFlavorSupported(java.awt.datatransfer.DataFlavor)
	 */
	public boolean isDataFlavorSupported( DataFlavor flavor )
	{
		return CADDIE_FLAVOR.equals( flavor );
	}

}
